<template>
  <div v-loading.fullscreen="loading">
    <el-dialog :visible="isShow" title="分配角色" @close="closeEvent">
      <el-checkbox-group v-model="checkArr">
        <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
          item.name
        }}</el-checkbox>
      </el-checkbox-group>
      <template #footer>
        <div>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="closeEvent">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysRole } from '@/api/setting.js'
import { sysUser } from '@/api/user.js'
import { sysUserAssignRoles } from '@/api/employees.js'
export default {
  name: 'DialogRole',
  data() {
    return {
      loading: false,
      isShow: false,
      list: [],
      checkArr: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    closeEvent() {
      this.isShow = false
      // 关闭清空checkArr
      this.checkArr = []
    },
    async getData() {
      const res = await sysRole({ pagesize: 300 })
      this.list = res.data.rows
      console.log(res)
    },
    async getUserInfo(id) {
      this.loading = true
      this.id = id
      const res = await sysUser(id)
      this.checkArr = res.data.roleIds
      this.loading = false
      console.log(res)
    },
    // 提交分配角色
    async submit() {
      await sysUserAssignRoles(this.id, this.checkArr)
      this.$message.success('分配角色成功！')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
